import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class creates  an interface for an agency or an Employer
 * Final Project for TCSS 445.
 * 
 * @author Sanjeev Kamboj
 */
@SuppressWarnings("serial")
public class FinalGUI extends JFrame {

	private static final JTextField my_company_name = new JTextField(20);
    private static final JTextField my_password = new JTextField(20);
    
    private static final JButton my_cancel_button = new JButton("Cancel");
      
    private SqlDao my_dao;
	private static JFrame my_frame;
	private JMenuBar my_menu_bar;
	private static final JMenu my_events = new JMenu("My Events");
	private static JMenu my_jobs = new JMenu ("My Jobs");
	
	private JPanel my_work_panel; //displays all actions
	private JobPostingPanel my_jobPost_panel; 
	private EventPostingPanel my_eventPost_panel; 
	private ShowPanel my_show_panel;
	
	private User my_user;
	 
	public FinalGUI() {
			
		makeConnection();		
		my_frame = new JFrame();
		my_frame.setLayout(new BorderLayout());
		my_frame.setSize(1360, 760);
        my_frame.setTitle("Employer/Agency System");
        my_user = new User();
        
        my_work_panel = new JPanel();
        my_jobPost_panel = new JobPostingPanel(my_frame, my_dao, my_user);
        my_eventPost_panel = new EventPostingPanel(my_frame, my_dao, my_user);
        my_show_panel = new ShowPanel(my_frame, my_dao, my_user);
        
      
        
        my_events.setEnabled(false);
        my_jobs.setEnabled(false);
        
        my_menu_bar = new JMenuBar();
        
        final JMenu my_login = new JMenu("Login");
        final JMenuItem login = new JMenuItem("Login"); // Quit sub item
        final JMenuItem quit = new JMenuItem("Quit");
        my_login.add(login);
        my_login.add(quit);
        
        
        final JMenuItem post_events = new JMenuItem("Post an Event");
        final JMenuItem get_events = new JMenuItem("See my Events");
        my_events.add(post_events);
        my_events.add(get_events);
        
        final JMenuItem post_jobs = new JMenuItem("Post a Job");
        final JMenuItem get_jobs = new JMenuItem("See my Jobs");
        my_jobs.add(post_jobs);
        my_jobs.add(get_jobs);
        
        my_menu_bar.add(my_login);
        my_menu_bar.add(my_events);
        my_menu_bar.add(my_jobs);
       
        my_frame.setJMenuBar(my_menu_bar);
        
        login.addActionListener(new ListenLogin());
        quit.addActionListener(new ListenMenuQuit());
        post_jobs.addActionListener(new PostJobListener());
        post_events.addActionListener(new PostEventListener());
        get_jobs.addActionListener(new getJobListener());
        get_events.addActionListener(new getEventListener());
        
        my_frame.addWindowListener(new CloseWindListener());
        
        my_frame.setVisible(true);
        
	}
	private class CloseWindListener extends WindowAdapter{
        public void windowClosing(WindowEvent e){
                System.exit(0);
        }
}
	
	private void makeConnection() {
		//Accessing Driver from JAR file
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Connection myCon = null;
		//Creates a connection with a variable called myCon
		try {
			myCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/FinalProject",
															"root", "");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		my_dao = new SqlDao(myCon);
		
	}
			
	
    private class ListenLogin implements ActionListener{
        public void actionPerformed(ActionEvent e){
                //Setup Login panel
                
                my_work_panel.removeAll();
                my_frame.remove(my_eventPost_panel);
        		my_frame.remove(my_jobPost_panel);
        		my_frame.remove(my_show_panel);
        		
        		my_frame.repaint();
        		my_frame.setVisible(true);
        		
                my_work_panel.setSize(300, 400);
                
                my_events.setEnabled(false);
                my_jobs.setEnabled(false);
                
                final JLabel input_label = new JLabel("Company Name: ");
                final JLabel input_label2 = new JLabel("Password: ");
                final JButton loginButton = new JButton("Submit"); 
                my_work_panel.add(input_label);
                my_work_panel.add(my_company_name);
                
                my_work_panel.add(input_label2);
                my_work_panel.add(my_password);
                
                my_work_panel.add(loginButton);
                my_work_panel.add(my_cancel_button);
                my_cancel_button.addActionListener(new ListenClose());
                loginButton.addActionListener(new checkLoginListener());
                my_frame.add(my_work_panel, BorderLayout.NORTH);
                my_frame.setJMenuBar(my_menu_bar);
                my_frame.setVisible(true);
        }
    }   
    private class checkLoginListener implements ActionListener{
        public void actionPerformed(ActionEvent e){                     
              final String name = my_company_name.getText();
              if (name.length() == 0) {
                JOptionPane.showMessageDialog(my_frame, "You must enter a company name");
              }
              else if(my_dao.isEmployer(name, my_password.getText())) {
            	  
            	  JOptionPane.showMessageDialog(my_frame, "Welcome " + name);
            	  
            	  my_user = new User(name, my_password.getText(), my_dao.getEmpID(name));
            	  my_events.setEnabled(true);
            	  my_jobs.setEnabled(true);
            	  
            	  my_frame.remove(my_work_panel);
            	  
            	  my_frame.setJMenuBar(my_menu_bar);
            	
            	  my_frame.setVisible(true);
            	  my_frame.repaint();
               
              } else {
            	  JOptionPane.showMessageDialog(my_frame, "Name and Password not found, please try again.");
            	  
              }
            	  /*else{
                  int n = JOptionPane.showConfirmDialog(
                            null, "Would you like to register?",
                            "Company Name Not found!",
                            JOptionPane.YES_NO_OPTION);

                  if(n == 0){
                            JOptionPane.showMessageDialog(null, "Registration not available at this time!!");
                  }else {}  
                 my_company_name.setText(null); // clears input field
              }*/
        }
    }
    
    private class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
                System.exit(0);
        }
    }
    private class ListenClose implements ActionListener{
        public void actionPerformed(ActionEvent e){
            my_frame.remove(my_work_panel);
            my_frame.repaint();
            my_frame.setVisible(true);
        }
    }
    
    private class PostJobListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        	my_frame.remove(my_eventPost_panel);
    		my_frame.remove(my_jobPost_panel);
    		my_frame.remove(my_show_panel);
    		
    		my_jobPost_panel = new JobPostingPanel(my_frame, my_dao, my_user);
    		
            my_frame.add(my_jobPost_panel, BorderLayout.CENTER);
            
            my_frame.repaint();
            my_frame.setVisible(true);
        }
    }
    private class PostEventListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        	my_frame.remove(my_eventPost_panel);
    		my_frame.remove(my_jobPost_panel);
    		my_frame.remove(my_show_panel);
    		
    		my_eventPost_panel = new EventPostingPanel(my_frame, my_dao, my_user);
    		
            my_frame.add(my_eventPost_panel, BorderLayout.CENTER);
                
                
            my_frame.repaint();
            my_frame.setVisible(true);
        }
    }
    private class getJobListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        	my_frame.remove(my_eventPost_panel);
    		my_frame.remove(my_jobPost_panel);
    		my_frame.remove(my_show_panel);
    		
    		my_show_panel = new ShowPanel(my_frame, my_dao, my_user);
    		my_show_panel.fillJobs();
    		
            my_frame.add(my_show_panel, BorderLayout.CENTER);
                
                
            my_frame.repaint();
            my_frame.setVisible(true);
        }
    }
    
    private class getEventListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        	my_frame.remove(my_eventPost_panel);
    		my_frame.remove(my_jobPost_panel);
    		my_frame.remove(my_show_panel);
    		
    		my_show_panel = new ShowPanel(my_frame, my_dao, my_user);
    		my_show_panel.fillEvents();
    		
            my_frame.add(my_show_panel, BorderLayout.CENTER);
                
                
            my_frame.repaint();
            my_frame.setVisible(true);
        }
    }
    
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				FinalGUI gui = new FinalGUI();
			}
		});
	}

}
